import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/basecomponent/CounterWidget.dart';
import 'package:flutter_app/util/AroutUtil.dart';
import 'package:flutter_app/util/DataUtil.dart';

import 'AlertDialogWidget.dart';
import 'FutureBuilderWidget.dart';
import 'StreamBuilderWidget.dart';

/// 功能组件list

class FunctionListWidget extends StatefulWidget {
  final String ? text;

  FunctionListWidget({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return _FunctionState();
  }
}

class _FunctionState extends State<FunctionListWidget> {
  var list = DataUtil.getFunctions();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.text??""),
      ),
      body: ListView.builder(
        itemCount: list.length,
        itemBuilder: _buildItem,
      ),
    );
  }
}

Widget _buildItem(BuildContext context, int index) {
  var list = DataUtil.getFunctions();
  return Container(
    padding: EdgeInsets.only(top: 20, left: 20, right: 20),
    alignment: Alignment.center,
    width: 100,
    height: 80,
    child: GestureDetector(
      child: DecoratedBox(
        decoration: BoxDecoration(
            gradient: LinearGradient(colors: [Colors.blue, Colors.orange]),
            //背景渐变
            borderRadius: BorderRadius.circular(3.0),
            boxShadow: [
              BoxShadow(
                  color: Colors.grey, offset: Offset(2.0, 2.0), blurRadius: 4.0)
            ]),
        child: Center(
          child: Text(
            list[index],
            style: TextStyle(fontSize: 20, color: Colors.white),
          ),
        ),
      ),
      onTap: () {
        toArount(index, context, list[index]);
      },
    ),
  );
}

void toArount(int index, BuildContext context, String title) {
  switch (index) {
    case 0:
      AroutUtil.goTo(context, FutureBuilderWidget(text: title));
      break;
    case 1:
      AroutUtil.goTo(context, StreamBuilderWidget(text: title));
      break;
    case 2:
      AroutUtil.goTo(context, AlertDialogWidget(text: title));
      break;
  }
}
